<script setup lang="jsx">
import {computed, inject, onMounted, ref, watch} from "vue";
import DataGrid from "@cps/DataGrid/DataGrid.vue";
import {Message} from "@arco-design/web-vue";
import declareColumns from "@/utils/shipmentTools.js";
import parcelDeclaration from "@/api/document/parcelDeclaration.js";
const getRecord = inject('getRecord');
const record = inject('record')
const visible = defineModel()
const userData = ref([])
const originalColumns = inject('originalDeclareColumns');

const parcelColumns = computed(() => {
  let columns = declareColumns.formatDeclareColumns(originalColumns.value, 'clearance')
  columns.unshift({title: '扩展编号', data: 'pivot.ext_number', align: 'center',readOnly:true,})
  columns.unshift({title: '货箱编号', data: 'number', align: 'center'})
  columns.map(i=>{
                        if(i.title == '带电' || i.title == '带磁'){
                            i.type = 'dropdown'
                            i.source = ['否', '是']
                        }
                    })
  return columns
})
onMounted(()=>{
  userData.value.splice(0, userData.value.length)
  for (let i = 0; i < record.value.parcel_declaration.length; i++) {
    let t = JSON.parse(JSON.stringify(record.value.parcel_declaration[i]))
    t.is_elec = t.is_elec == 1 ? '是':'否'
    t.is_magnetic = t.is_magnetic == 1 ? '是':'否'
    userData.value.push(t)
  }
})
const submit = async () => {
  let res = await parcelDeclaration.updateBatch({
    declaration: userData.value.filter(i => i.number),
  })
  if (!res.success) {
    Message.error(res.message)
    return false;
  }
  Message.info(res.message)
  getRecord();
  visible.value = false
  return false;
}
const close = () => {
  visible.value = false
  userData.value = [];
}
</script>

<template>
  <a-drawer
      :visible="visible"
      title="申报信息"
      width="70%"
      :destroyOnClose="true"
      :getContainer="false"
      :style="{position: 'absolute'}"
      @cancel="close"
      @ok="submit"
  >
    <data-grid :columns="parcelColumns" v-model="userData"/>
  </a-drawer>
</template>

<style scoped>

</style>